using System;
using System.Collections.Generic;
using UnityEngine;

public class LevelDataIndex : ScriptableObject
{
	[Serializable]
	private class LevelTrace
	{
		public int chapter;

		public int level;

		public override bool Equals(object obj)
		{
			if (!(obj is LevelTrace))
			{
				return false;
			}
			return ((LevelTrace)obj).chapter == chapter;
		}

		public override int GetHashCode()
		{
			return chapter;
		}
	}

	public static LevelData CurrentLevel;

	[SerializeField]
	private List<LevelData> m_Indexes;

	[SerializeField]
	private List<LevelTrace> m_LevelTraces = new List<LevelTrace>();

	public int Count => m_Indexes.Count;

	public LevelData this[int index]
	{
		get
		{
			if (index < m_Indexes.Count)
			{
				return m_Indexes[index];
			}
			return null;
		}
	}

	public LevelData this[string index]
	{
		get
		{
			for (int i = 0; i < Count; i++)
			{
				if (m_Indexes[i].FileName.CompareTo(index) == 0)
				{
					return m_Indexes[i];
				}
			}
			return null;
		}
	}

	public LevelDataIndex()
	{
		m_Indexes = new List<LevelData>();
	}

	public void TracingLevel(string levelName)
	{
		string[] array = levelName.Split('-');
		int num = int.Parse(array[0]);
		int level = int.Parse(array[1]);
		int i = 0;
		for (int count = m_LevelTraces.Count; i < count; i++)
		{
			if (m_LevelTraces[i].chapter == num)
			{
				m_LevelTraces[i].level = level;
				return;
			}
		}
		LevelTrace levelTrace = new LevelTrace();
		levelTrace.chapter = num;
		levelTrace.level = level;
		LevelTrace item = levelTrace;
		m_LevelTraces.Add(item);
	}

	public int GetLevelTraceInt(int chapter)
	{
		int i = 0;
		for (int count = m_LevelTraces.Count; i < count; i++)
		{
			if (m_LevelTraces[i].chapter == chapter)
			{
				return m_LevelTraces[i].level;
			}
		}
		return 1;
	}

	public void ClearLevelTraceData()
	{
		m_LevelTraces.Clear();
	}

	public void Add(LevelData data)
	{
		if (!m_Indexes.Contains(data))
		{
			m_Indexes.Add(data);
			m_Indexes.Sort(Comp);
		}
	}

	public LevelData Next(LevelData data)
	{
		int num = m_Indexes.IndexOf(data);
		return this[num + 1];
	}

	public void Clear()
	{
		m_Indexes.Clear();
	}

	public int Comp(string l, string r)
	{
		string[] array = l.Split('-');
		string[] array2 = r.Split('-');
		if (array.Length != 2 || array2.Length != 2)
		{
			throw new Exception("Occur error . left name : " + l + " , right name : " + r);
		}
		int num = int.Parse(array[0]);
		int num2 = int.Parse(array2[0]);
		if (num < num2)
		{
			return -1;
		}
		if (num > num2)
		{
			return 1;
		}
		int num3 = int.Parse(array[1]);
		int num4 = int.Parse(array2[1]);
		if (num3 < num4)
		{
			return -1;
		}
		if (num3 > num4)
		{
			return 1;
		}
		return 0;
	}

	public int Comp(LevelData l, LevelData r)
	{
		string fileName = l.FileName;
		string fileName2 = r.FileName;
		return Comp(fileName, fileName2);
	}

	public int GetLevel(string levelDataName)
	{
		return int.Parse(levelDataName.Split('-')[1]);
	}

	public int GetChapter(string levelDataName)
	{
		return int.Parse(levelDataName.Split('-')[0]);
	}

	public IList<LevelData> DumpLevelDatasWithChapter(int chapter)
	{
		List<LevelData> list = new List<LevelData>();
		for (int i = 0; i < m_Indexes.Count; i++)
		{
			if (GetChapter(m_Indexes[i].FileName) == chapter)
			{
				list.Add(m_Indexes[i]);
			}
		}
		return list;
	}

	public int GetIndex(string fileName)
	{
		if (!fileName.Contains("-"))
		{
			return -1;
		}
		string[] array = fileName.Split('-');
		int num = int.Parse(array[0]);
		int num2 = int.Parse(array[1]);
		return (num - 1) * 20 + num2;
	}
}
